#ifndef dsproxy_command_handler_h__
#define dsproxy_command_handler_h__

#include "IObject/IServerObject.h"
#include "IObject/IPropertyParser.h"
#include "IObject/ISessionManager.h"
#include "IObject/ITimerManager.h"
#include "IObject/ILogger.h"
#include "CSCodec.h"
#include <boost/atomic.hpp>
#include <string>
#include "boost/thread/shared_mutex.hpp"
#include <list>
namespace DSProxy
{
	class command_handler
	{
	public:
		command_handler();
		~command_handler();

	protected:
		void OnClientConnected(SocketID socketID, int cmd,
			const void * pBuffer, uint32_t size);

		void OnClientDisconnected(SocketID socketID, int cmd,
			const void * pBuffer, uint32_t size);

		void OnMessage_100(SocketID socketID, int cmd,
			const void * pBuffer, uint32_t size);

		void OnRemoteMessage(SocketID socketID, int cmd,
			const void * pBuffer, uint32_t size);
	public:
		void Init(const char* pszModuleName, IServerObject * pServerObj);
		void Fina();

	protected:
		void login(SocketID socketID);
		void keepalive(TimerID id);

		void RemoveClient(SocketID	socketID);
		void CloseClientByRemoteID(SocketID	remoteID);
		SocketID GetValidRemoteServer();

	protected:
		IServerObject  *m_pServerObject;
		ILogger		   *m_pLogger;
		CSCodec        *m_pCSCodec;

		std::string		m_ServerToken;
		boost::shared_mutex							m_rw_lock_clients;
		std::list< std::pair<SocketID, SocketID> >  m_SocketPair;

		std::vector< std::pair<SocketID, boost::atomic_bool> >	m_RemoteServerIDS;
		boost::atomic_int m_index;

		TimerID		m_timerRemoteKeepAlive;
	};
}


#endif // dsproxy_command_handler_h__
